//
//  CustomControl.m
//  自定义空间
//
//  Created by MyMac on 15/7/15.
//  Copyright (c) 2015年 MyMac. All rights reserved.
//

#import "CustomControl.h"
#define TB_BACKGROUND_WIDTH 25.0f
#define radius self.frame.size.height/2-TB_BACKGROUND_WIDTH/2
#define TB_LINE_WIDTH TB_BACKGROUND_WIDTH-5.0f
#define MinDistance MIN(self.frame.size.width, self.frame.size.height)
#define ToRad(deg) 		( (M_PI * (deg)) / 180.0 )//旋转的角度
#define ToDeg(rad)		( (180.0 * (rad)) / M_PI )
#define SQR(x)			( (x) * (x) )

@implementation CustomControl


- (void)drawRect:(CGRect)rect {
   // self.angle=90;
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextAddArc(ctx,MinDistance/2.0,MinDistance/2.0,radius, 0, M_PI *2,1);
    
   // CGContextAddArc(ctx, 100, 100, 100,0, M_PI, 0);
    //半径是以长减去线宽的一半，，，以线宽的中点到图形中点的距离
    //Set the stroke colour
    [[UIColor blackColor]setStroke];
    
    //set Line width and cap
    CGContextSetLineWidth(ctx,TB_BACKGROUND_WIDTH);
    CGContextSetLineCap(ctx, kCGLineCapButt);
    //draw it!
    CGContextDrawPath(ctx, kCGPathStroke);

    CGImageRef imageRef=[self receiveCurrentImage];
    // CGContextSaveGState(ctx);//保存上下文
    //按照照片裁剪，保留非透明部分，前面绘制的都还存在，后面的按照存在的部分画上去
    CGContextClipToMask(ctx, rect, imageRef);
    
    CGImageRelease(imageRef);
//    UIImage*image=[UIImage imageWithCGImage:imageRef];
//
//    [image drawAtPoint:CGPointMake(0,0)];
    
    CGFloat components[12] = {0.0, 0.0, 1.0, 1.0,
        1.0, 0.0, 1.0, 1.0, };//开始蓝色，，最后结束紫色
    CGColorSpaceRef baseSpace=CGColorSpaceCreateDeviceRGB();//设置色域，用RGB算颜色
   // CGFloat location[3]={1.0,0.5,0.8};
    CGGradientRef gradient=CGGradientCreateWithColorComponents(baseSpace, components, NULL, 2);//设置一个颜色梯度，渐变效果，2表示两个颜色间渐变
    CGPoint startPoint = CGPointMake(0.0, 0.0);//开始的位置
    CGPoint endPoint = CGPointMake(0.0, rect.size.height);//结束的位置
    CGColorSpaceRelease(baseSpace), baseSpace = NULL;//释放，，凡事以Create创建的都得手动释放

    //绘制梯度，填充渐变，填充后是一个矩形/在一个上下文中创建渐变色
   CGContextDrawLinearGradient(ctx, gradient, startPoint, endPoint, 0);
    CGGradientRelease(gradient), gradient = NULL;
   ;
//    CGContextAddArc(ctx, 50, 50, 80, 0.0, 50, 1);
//    CGContextStrokePath(ctx);
  //  CGContextRestoreGState(ctx);
    [self drawTheHandle:ctx];
   // CGContextStrokePath(ctx);
}
-(CGImageRef)receiveCurrentImage
{
    UIGraphicsBeginImageContext(self.bounds.size);
    
    CGContextRef imageCtx = UIGraphicsGetCurrentContext();
    CGContextAddArc(imageCtx,MinDistance/2.0,MinDistance/2.0,radius, 0,ToRad(self.angle),0);//1是顺时针
   
    [[UIColor redColor ]set];
    
    //设置阴影，，，显示模糊效果
    CGContextSetShadowWithColor(imageCtx, CGSizeMake(0, 0), self.angle/20, [UIColor colorWithRed:150/255.0 green:200/255.0f blue:100/255.0f alpha:1.0].CGColor);
    
    //define the path
    CGContextSetLineWidth(imageCtx, TB_LINE_WIDTH);
    CGContextDrawPath(imageCtx, kCGPathStroke);
    
    //save the context content into the image mask
    CGImageRef mask = CGBitmapContextCreateImage(imageCtx);
    UIGraphicsEndImageContext();
    //CGImageRelease(mask);
    return mask;

}
/**
 *  根据角度算点的位置
 *
 */
-(CGPoint)pointFromAngle:(NSInteger)angleInt{
    
    //Define the Circle center
    CGPoint centerPoint = CGPointMake(self.frame.size.width/2 - TB_LINE_WIDTH/2, self.frame.size.height/2 - TB_LINE_WIDTH/2);
    
    //Define The point position on the circumference
    CGPoint result;
    result.y = round(centerPoint.y +radius * sin(ToRad(-angleInt))) ;
    result.x = round(centerPoint.x +radius * cos(ToRad(-angleInt)));
    
    return result;
}
//绘制手柄
-(void) drawTheHandle:(CGContextRef)ctx{
    
    CGContextSaveGState(ctx);
    
    //I Love shadows
    CGContextSetShadowWithColor(ctx, CGSizeMake(0, 0), 3, [UIColor blackColor].CGColor);
    
    //Get the handle position!
    CGPoint handleCenter =  [self pointFromAngle: self.angle];
    
    //Draw It!
    [[UIColor colorWithWhite:1.0 alpha:0.7]set];
    CGContextFillEllipseInRect(ctx, CGRectMake(handleCenter.x, handleCenter.y, TB_LINE_WIDTH, TB_LINE_WIDTH));
    
    CGContextRestoreGState(ctx);
}
- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event;
{
    [super beginTrackingWithTouch:touch withEvent:event];
      NSLog(@"开始追踪");
    return YES;

}
- (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event;
{
    [super continueTrackingWithTouch:touch withEvent:event];
  NSLog(@"继续追踪");
    [self sendActionsForControlEvents:UIControlEventValueChanged];
    return YES;
}
- (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event;
{
    [super endTrackingWithTouch:touch withEvent:event];
  NSLog(@"结束追踪");
}
- (void)cancelTrackingWithEvent:(UIEvent *)event;
{
    [super cancelTrackingWithEvent:event];

    NSLog(@"取消追踪");


}
@end
